<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        li {
            width: 200px;
            height: 20px;
            border: tomato 1px solid;
            background: #222222;
            list-style: none;
            margin-bottom: 5px;
        }
    </style>
</head>

<body>
    <!-- ## 2.循环生成列表数据，然后使用js实现隔行变色 -->
    行：<input type="text" id="num">
    <button id="btn">生成li</button>
    <ul id="ul">

    </ul>
    <script>
        //生成li
        btn.onclick = function () {
            var n = num.value
            var res = ''
            for (var i = 0; i < n; i++) {
                res += '<li></li>'
            }
            ul.innerHTML = res
            //获取所有li
            var ali = document.getElementsByTagName('li')
            for (var i = 0; i < ali.length; i++) {
                //判断i是否为整数  更换颜色
                if (i % 2 === 0) {
                    ali[i].style.background = 'red';
                } else {
                    ali[i].style.background = 'blue';
                }
            }
        }


    </script>
</body>

</html>